//
// Created by Tan Ke on 2024/2/20.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int cmp(const void* p, const void* q)
{
    int val_1 = *((int*)p);
    int val_2 = *((int*)q);

    return val_1 - val_2;
}

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target)
{
    bool found = false;
    for (int i = 0; i < matrixSize; i ++) {
        if (matrix[i][0] > target) {
            break;
        }
        int *q = bsearch(&target, matrix[i],  *matrixColSize, sizeof(int), cmp);
        if (q != NULL) {
            found = true;
            break;
        }
    }

    return found;
}

int main(void)
{
    int **nums = (int**)malloc(sizeof(int*) * 3);
    for (int i = 0; i < 3; i ++) {
        nums[i] = (int*)malloc(sizeof(int) * 4);
    }
    int col = 4;

    nums[0][0] = 1;
    nums[0][1] = 3;
    nums[0][2] = 5;
    nums[0][3] = 7;
    nums[1][0] = 10;
    nums[1][1] = 11;
    nums[1][2] = 16;
    nums[1][3] = 20;
    nums[2][0] = 23;
    nums[2][1] = 30;
    nums[2][2] = 34;
    nums[2][3] = 60;

    printf("%d\n", searchMatrix(nums, 3, &col, 3));

    return 0;
}

